# coding=utf-8
import datetime
import re
import time
from enum import Enum

FORMAT_DATE_NORMAL = "%Y-%m-%d %H:%M:%S"
FORMAT_DATE_ONLY = "%Y-%m-%d"
FORMAT_DATE_UTC = "%Y-%m-%dT%H:%M:%SZ"


class TimeUnit(Enum):
    DAY = 1
    HOUR = 2
    MINUTE = 3
    SECOND = 4


def parse_date(src_datestr, formatter):
    return datetime.datetime.strptime(src_datestr, formatter)


def format_date(src_date, formatter):
    return src_date.strftime(formatter)


def today():
    return datetime.date.today()


def get_future_date(srcdate, datediff, time_unit):
    if time_unit == TimeUnit.DAY:
        return srcdate + datetime.timedelta(days=datediff)
    elif time_unit == TimeUnit.HOUR:
        return srcdate + datetime.timedelta(hours=datediff)
    elif time_unit == TimeUnit.MINUTE:
        return srcdate + datetime.timedelta(minutes=datediff)
    elif time_unit == TimeUnit.SECOND:
        return srcdate + datetime.timedelta(seconds=datediff)
    else:
        raise Exception("不支持的单位类型")


def get_time_different(old_date, new_date, time_unit):
    if time_unit == TimeUnit.DAY:
        return (new_date - old_date).days
    elif time_unit == TimeUnit.HOUR:
        return (new_date - old_date).seconds / 60 / 60
    elif time_unit == TimeUnit.MINUTE:
        return (new_date - old_date).seconds / 60
    elif time_unit == TimeUnit.SECOND:
        return (new_date - old_date).seconds
    else:
        raise Exception("不支持的单位类型")


def yesterday():
    return get_future_date(datetime.date.today(), -1, TimeUnit.DAY)


def tomorrow():
    return get_future_date(datetime.date.today(), 1, TimeUnit.DAY)


def after_tomorrow():
    return get_future_date(datetime.date.today(), 2, TimeUnit.DAY)


def str2date(datestr):
    """
    convert datestr to datetime.date
    '2015-04-05' ==> 2015-04-05
    """
    date_array = datestr.split('-')
    return datetime.date(int(date_array[0]), int(date_array[1]), int(date_array[2]))


def date2str(src_date, formatter=FORMAT_DATE_ONLY):
    return format_date(src_date, formatter)


def datetime2str(src_datetime, formatter=FORMAT_DATE_NORMAL):
    return format_date(src_datetime, formatter)


def timestamp2str(src_timestamp, fromatter=FORMAT_DATE_NORMAL):
    return time.strftime(fromatter, time.localtime(src_timestamp))


def local2UTC(lt):
    '''local time to utc'''
    time_struct = time.mktime(lt.timetuple())
    utc = datetime.datetime.utcfromtimestamp(time_struct)
    return utc


def now_str():
    return datetime2str(datetime.datetime.now())


def now():
    return datetime.datetime.now()


def now_utc_str():
    return datetime2str(local2UTC(datetime.datetime.now()), FORMAT_DATE_UTC)


def daysdiff(old_date, new_date):
    if old_date == new_date:
        return 0
    return int(re.search('(-?\d+)\s+day', str(new_date - old_date)).group(1))


def get_now_time():
    return time.strftime(FORMAT_DATE_NORMAL, time.localtime(time.time()))


def get_current_timestamp():
    return int(time.time() * 1000)


if __name__ == "__main__":
    print(format_date(datetime.datetime.now(), "%Y%m%d%H%M%S%f")[:-3])
    print((datetime.datetime.now() - datetime.timedelta(seconds=30)).strftime("%Y-%m-%d %H:%M:%S"))
    print(date2str(today()))
    print(date2str(today()) == '2019-05-27')
    print(parse_date(get_now_time(), FORMAT_DATE_NORMAL).hour)
    depTime = "01:23"
    print(int(depTime.split(":")[0]) < 6)
    print(date2str(today()))
    print(time.time_ns())
    print(get_current_timestamp())
    print(time.time())
